﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NGE;


namespace MH.ME
{
    public partial class Form1 : Form
    {
        List<Character> mons;
        Character mon;
        NAnimation nanibuff; 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("data\\character\\mons.nchrs"))
            {
                mons = (List<Character>)Util.LoadObject("data\\character\\mons.nchrs");
            }
            else
            {
                mons = new List<Character>();
            }
            mon = new Character();
            refresh_anilist();
            refresh_list();
        }
        void refresh_anilist()
        {
            string addstr = "不存在";
            if (mon.AvatarReady != null)
            {
                addstr = "存在";
            }
            radioButton1.Text = "待命-" + addstr;

            addstr = "不存在";
            if (mon.AvatarFight != null)
            {
                addstr = "存在";
            }
            radioButton2.Text = "攻击-" + addstr;

            addstr = "不存在";
            if (mon.AvatarWeapon != null)
            {
                addstr = "存在";
            }
            radioButton3.Text = "武器-" + addstr;

            addstr = "不存在";
            if (mon.AvatarItem != null)
            {
                addstr = "存在";
            }
            radioButton4.Text = "道具-" + addstr;

            addstr = "不存在";
            if (mon.AvatarHit != null)
            {
                addstr = "存在";
            }
            radioButton5.Text = "被击中-" + addstr;
            nanibuff = null;
            pictureBox1.Refresh();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (mons == null)
            {
                return;
            }
            Util.SaveObject("data\\character\\mons.nchrs", mons);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                nanibuff = (NAnimation)Util.LoadObject(fd.FileName);
                //trackBar1.Maximum = nanibuff.FramesCount;
                //label12.Text = "/" + trackBar1.Maximum;

                //label14.Text = "X: " + nanibuff.Offset.X + " Y: " + nanibuff.Offset.Y;

                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            if (nanibuff == null)
            {                
                return;
            }
            
            nanibuff.CurrentFrame.Init();
            g.DrawImage(nanibuff.CurrentFrame.MemImage, new Point());
            nanibuff.NextFrameLoop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (nanibuff == null)
            {
                return;
            }
            timer1.Enabled = false;
            timer1.Interval = nanibuff.CurrentFrameDelay;
            timer1.Enabled = true;
            pictureBox1.Refresh();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                mon.AvatarReady = nanibuff;
            } 
            if (radioButton2.Checked)
            {
                mon.AvatarFight = nanibuff;
            }
            if (radioButton3.Checked)
            {
                mon.AvatarWeapon = nanibuff;
            }
            if (radioButton4.Checked)
            {
                mon.AvatarItem = nanibuff;
            }
            if (radioButton5.Checked)
            {
                mon.AvatarHit = nanibuff;
            }
            refresh_anilist();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                nanibuff = mon.AvatarReady;
            }
            if (radioButton2.Checked)
            {
                nanibuff = mon.AvatarFight;
            }
            if (radioButton3.Checked)
            {
                nanibuff = mon.AvatarWeapon;
            }
            if (radioButton4.Checked)
            {
                nanibuff = mon.AvatarItem;
            }
            if (radioButton5.Checked)
            {
                nanibuff = mon.AvatarHit;
            }
            pictureBox1.Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    mon.MonsterDamageA = double.Parse(textBox1.Text);
                    mon.MonsterDamageAType = int.Parse(textBox3.Text);
                    if (comboBox2.SelectedIndex == 0)
                    {
                        mon.MonsterAttackA1 = nanibuff;
                        
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        mon.MonsterAttackA2 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        mon.MonsterAttackA3 = nanibuff;
                    }
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    mon.MonsterDamageB = double.Parse(textBox1.Text);
                    mon.MonsterDamageBType = int.Parse(textBox3.Text);
                    if (comboBox2.SelectedIndex == 0)
                    {
                        mon.MonsterAttackB1 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        mon.MonsterAttackB2 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        mon.MonsterAttackB3 = nanibuff;
                    }
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    mon.MonsterDamageC = double.Parse(textBox1.Text);
                    mon.MonsterDamageCType = int.Parse(textBox3.Text);
                    if (comboBox2.SelectedIndex == 0)
                    {
                        mon.MonsterAttackC1 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        mon.MonsterAttackC2 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        mon.MonsterAttackC3 = nanibuff;
                    }
                }
                else if (comboBox1.SelectedIndex == 3)
                {
                    mon.MonsterDamageD = double.Parse(textBox1.Text);
                    mon.MonsterDamageDType = int.Parse(textBox3.Text);
                    if (comboBox2.SelectedIndex == 0)
                    {
                        mon.MonsterAttackD1 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        mon.MonsterAttackD2 = nanibuff;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        mon.MonsterAttackD3 = nanibuff;
                    }
                }
            }
            catch { }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    if (comboBox2.SelectedIndex == 0)
                    {
                        nanibuff = mon.MonsterAttackA1;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        nanibuff = mon.MonsterAttackA2;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        nanibuff = mon.MonsterAttackA3;
                    }
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    if (comboBox2.SelectedIndex == 0)
                    {
                        nanibuff = mon.MonsterAttackB1;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        nanibuff = mon.MonsterAttackB2;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        nanibuff = mon.MonsterAttackB3;
                    }
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    if (comboBox2.SelectedIndex == 0)
                    {
                        nanibuff = mon.MonsterAttackC1;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        nanibuff = mon.MonsterAttackC2;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        nanibuff = mon.MonsterAttackC3;
                    }
                }
                else if (comboBox1.SelectedIndex == 3)
                {
                    if (comboBox2.SelectedIndex == 0)
                    {
                        nanibuff = mon.MonsterAttackD1;
                    }
                    else if (comboBox2.SelectedIndex == 1)
                    {
                        nanibuff = mon.MonsterAttackD2;
                    }
                    else if (comboBox2.SelectedIndex == 2)
                    {
                        nanibuff = mon.MonsterAttackD3;
                    }
                }
            }
            catch { }
            pictureBox1.Refresh();
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                textBox1.Text = mon.MonsterDamageA + "";
                textBox3.Text = mon.MonsterDamageAType + "";
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                textBox1.Text = mon.MonsterDamageB + "";
                textBox3.Text = mon.MonsterDamageBType + "";
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                textBox1.Text = mon.MonsterDamageC + "";
                textBox3.Text = mon.MonsterDamageCType + "";
            }
            else if (comboBox1.SelectedIndex == 3)
            {
                textBox1.Text = mon.MonsterDamageD + "";
                textBox3.Text = mon.MonsterDamageDType + "";
            }

        }

        void refresh_list()
        {
            listBox1.Items.Clear();
            for (int i = 0; i < mons.Count; i++)
            {
                listBox1.Items.Add(mons[i].Name);
            }
            listBox1.Items.Add("[新建]");
            
        }

        private void button3_Click(object sender, EventArgs e)
        {

            mon.Name = textBox4.Text;
            mon.LVL =int.Parse( textBox5.Text);
            mon.MonsterDefense =double.Parse( textBox6.Text);
            mon.MonsterAccurate =double.Parse( textBox7.Text);
            mon.MonsterMissrate =double.Parse( textBox8.Text);
            mon.HP = int.Parse(textBox9.Text);
            mon.HPMax = int.Parse(textBox10.Text);
            mon.EXP = int.Parse(textBox11.Text);
            mon.AP = int.Parse(textBox12.Text);
            mon.MonsterDamage = double.Parse(textBox13.Text);


            if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
            {
                mons.Add(mon);
            }
            else
            {
                mons[listBox1.SelectedIndex] = mon;
            }
            refresh_list();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
            {
                mon = new Character();
            }
            else
            {
                mon = mons[listBox1.SelectedIndex];
            }

            textBox4.Text = "" + mon.Name;
            textBox5.Text = "" + mon.LVL;
            textBox6.Text = "" + mon.MonsterDefense;
            textBox7.Text = "" + mon.MonsterAccurate;
            textBox8.Text = "" + mon.MonsterMissrate;
            textBox9.Text = "" + mon.HP;
            textBox10.Text = "" + mon.HPMax;
            textBox11.Text = "" + mon.EXP;
            textBox12.Text = "" + mon.AP;
            textBox13.Text = "" + mon.MonsterDamage;

            refresh_anilist();
        }
    }
}
